{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-07-25T03:45:09.794734700Z",
     "start_time": "2024-07-25T03:45:09.787235500Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "src:\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "dst:\n",
      "[[0.09003057 0.24472847 0.66524096]\n",
      " [0.09003057 0.24472847 0.66524096]\n",
      " [0.09003057 0.24472847 0.66524096]]\n",
      "max:\n",
      "[[3]\n",
      " [6]\n",
      " [9]]\n",
      "sum:\n",
      "[[1.50321472]\n",
      " [1.50321472]\n",
      " [1.50321472]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "def softmax(src):\n",
    "    #基于last轴进行rowmax(按行取最大值)处理\n",
    "    max = np.max(src, axis=-1, keepdims=True)\n",
    "    sub = src - max\n",
    "    exp = np.exp(sub)\n",
    "    #基于last轴进行rowsum(按行求和)处理\n",
    "    sum = np.sum(exp, axis=-1, keepdims=True)\n",
    "    dst = exp / sum\n",
    "    return dst, max, sum\n",
    "\n",
    "input = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
    "\n",
    "dst, max, sum = softmax(input)\n",
    "\n",
    "print(f'src:\\n{input}')\n",
    "print(f'dst:\\n{dst}')\n",
    "print(f'max:\\n{max}')\n",
    "print(f'sum:\\n{sum}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "0.7167022912550333\n",
      "0.831\n"
     ]
    }
   ],
   "source": [
    "print(np.random.randint(0, 2))\n",
    "print(np.random.rand())\n",
    "print(np.float16(np.random.rand()))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-26T01:41:17.113142500Z",
     "start_time": "2024-07-26T01:41:17.100183400Z"
    }
   },
   "id": "17796b11c537e907"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float16)"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.zeros(10).astype(np.float16)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-07-26T01:32:49.796161900Z",
     "start_time": "2024-07-26T01:32:49.783943300Z"
    }
   },
   "id": "4f2590419d79dd8e"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "3ddb79fe8b60d27a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
